<template>
  <div class="login">
    <!-- <h3 class="login-title">登录界面</h3> -->

    <transition mode="out-in">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            show-password
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <el-checkbox v-model="form.remember">在本设备保存登录状态</el-checkbox>
          <a class="fr" href="/">忘记密码</a>
        </el-form-item>-->
        <el-form-item class="login-button-wrapper">
          <el-button class="login-button" type="primary" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
        <el-form-item>
          <span style="color: #7f7f7f; ">
            <!-- 还没账号？去
            <router-link :to="{ name: 'Register', query: $route.query }">
              注册
            </router-link>-->
            登录名为用户ID,初始密码为123456,登录后请及时修改密码
          </span>
        </el-form-item>
      </el-form>
    </transition>
  </div>
</template>

<script>
import { login } from '@/api/user';
import { setToken } from '@/libs/util';

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: ['blur']
          },
          {
            min: 6,
            max: 11,
            message: '长度在 6 到 11 个数字',
            trigger: ['blur']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur']
          },
          {
            min: 6,
            max: 20,
            message: '长度在 6 到 20 个字符',
            trigger: ['blur']
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          login(this.form)
            .then(res => {
              console.log(res, "res");
              if (res.result) {
                setToken(res.data.token, 1);
                this.loading = false
                if (res.data.role[0] == 'student') {
                  this.$router.replace(this.redirect || '/timetable')
                } else {
                  this.$router.replace(this.redirect || '/course')
                }
                this.$message.success(res.message);
                // this.$router.replace(this.redirect || '/')
              } else {
                this.loading = false
                this.$message.error(res.message)
              }
            })
            .catch(error => {
              console.log(error)
              this.$message({
                message: error.message,
                type: 'warning'
              })
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    initRedirect() {
      this.redirect = this.$route.query && this.$route.query.redirect
    }
  },
  created() {
    this.initRedirect()
  }
}
</script>

<style lang="stylus" scoped>
.login
  width 410px
  margin 120px auto 0
  .login-title
    margin-bottom 20px
    font-size 16px
    font-weight 600
  .login-button-wrapper
    margin-bottom 0
    .login-button
      width 100%
  .login_header {
    display flex
    align-items center
    justify-content center
    margin 150px 0 30px 0
    h3 {
      width 150px
      text-align center
      position relative
      cursor pointer
    }
    .active {
      color #1890ff
      &::after {
        position absolute
        bottom -10px
        left 0
        content ''
        width 100%
        height 2px
        background #1890ff
      }
    }
  }
</style>
